﻿using System;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Hsenl {
    [DisallowMultipleComponent]
    public class DatabaseProxy : MonoBehaviour {
        private DatabaseManager _databaseManager;
        public string repositoriesRootDir;

        private void Awake() {
            if (!SingletonManager.IsDisposed<DatabaseManager>()) {
                SingletonManager.Unregister<DatabaseManager>();
            }

            SingletonManager.Register(ref this._databaseManager);

            if (string.IsNullOrEmpty(this.repositoriesRootDir)) {
                throw new Exception("Repositories Root Directory Is Empty!");
            }

            this._databaseManager.SetRoot(this.repositoriesRootDir);
        }
    }

#if UNITY_EDITOR
    [CustomEditor(typeof(DatabaseProxy))]
    public class DatabaseProxyEditor : Editor {
        private DatabaseProxy _t;

        protected void OnEnable() {
            this._t = (DatabaseProxy)this.target;
        }

        public override void OnInspectorGUI() {
            base.OnInspectorGUI();
            if (string.IsNullOrEmpty(this._t.repositoriesRootDir)) {
                this._t.repositoriesRootDir = Application.persistentDataPath + "/DBRepositories";
                EditorUtility.SetDirty(this._t);
            }
        }
    }
#endif
}